#include "testing.h"

using namespace OBR;

std::string vec2string(float3 v)
{
    char buff[64];
    sprintf(buff, "[%.6f, %.6f, %.6f]", v.x, v.y, v.z);
    return buff;
}

std::string vec2string(float4 v)
{
    char buff[64];
    sprintf(buff, "[%.6f, %.6f, %.6f, %.6f]", v.x, v.y, v.z, v.w);
    return buff;
}

void __test_dynamic_transform()
{
    // initialize factories
    Configurator::initProgram();

    // load scene from config
    Configurator configurator(
        "D:/CourseCode/OptixBasedRenderer/assets/scenes/motion-blur/#scene_desc_static.json");
    Scene sc = configurator.getScene();

    // setup scene
    sc.setupLaunchParams();
    sc.setupRenderer();
    sc.enable_accumulate = false;

    std::shared_ptr<SRTAnimation> animation = std::dynamic_pointer_cast<SRTAnimation>(sc.rootAS);
    std::shared_ptr<SRTTransform> &trans = animation->transforms[0];

    // render
    double t = 0;
    double fps = -1;
    int key = -1;
    while (key != 27)
    {
        double dt;

        auto t0 = std::chrono::steady_clock::now();
        sc.renderFrame();
        auto t1 = std::chrono::steady_clock::now();
        dt = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() / 1e9;

        if (fps == -1)
        {
            fps = 1.0 / dt;
        }
        else
        {
            fps = fps * 0.9 + 1.0 / dt * 0.1;
        }
        char desc[128] = {};
        sprintf(desc, "fps: %.2f", fps);

        DefaultUI::show(sc, "testing dynamic object", desc);

        float theta = t * 0.2;
        trans->rotation = Quaternion(0, 0, sinf(theta), cosf(theta));
        animation->updateAccelStructure();

        key = cv::waitKey(10);
        t += dt;
    }
}

void OBR::testing()
{
    __test_dynamic_transform();
}